package com.heifeng.cloudshelf.mvp.search.activity;

import android.content.Context;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.heifeng.cloudshelf.NetWorkReceiver;
import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.base.BaseActivity;
import com.heifeng.cloudshelf.constants.HTTPConstants;
import com.heifeng.cloudshelf.dao.SearchDefaultBean;
import com.heifeng.cloudshelf.event.NoSearchDataEvent;
import com.heifeng.cloudshelf.event.SreachNoDataEvent;
import com.heifeng.cloudshelf.mvp.search.contract.SearchHomeActivityContract;
import com.heifeng.cloudshelf.mvp.search.presenter.SearchHomeActivityPresenter;
import com.heifeng.cloudshelf.utils.IntentUtil;
import com.heifeng.cloudshelf.utils.LogUtils;
import com.orhanobut.hawk.Hawk;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/*
 *搜搜我家
 */
public class SearchHomeActivity extends BaseActivity<SearchHomeActivityPresenter> implements SearchHomeActivityContract.View {
    @BindView(R.id.l_error)
    View lError;//网络错误

    @BindView(R.id.l_no_data)
    View lNoData;//没有数据

    @BindView(R.id.r_top)
    RelativeLayout rTop;

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;

    @BindView(R.id.l_back)
    LinearLayout lBack;

    @BindView(R.id.l_bottom)
    LinearLayout lBottom;

    @BindView(R.id.tv_search_text)
    TextView tvSearchText;

    @BindView(R.id.tv_list_count)
    TextView tv_list_count;

    @BindView(R.id.iv_search)
    ImageView ivSearch;

    @BindView(R.id.iv_image)
    ImageView noDataImage;

    @BindView(R.id.tv_text)
    TextView noDataText;

    private TranslateAnimation translateAnimation;

    private TranslateAnimation translateAnimation1;

    private TranslateAnimation translateAnimation2;

    private String listCount = "0个方案结果";

    public static void open(Context context) {
        IntentUtil.startActivity(context, SearchHomeActivity.class);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_search_home;
    }

    @Override
    protected void initEventAndData() {
        onStartAnim();
        EventBus.getDefault().register(this);

        lError.findViewById(R.id.tv_refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkNet();
                showNetError(isNetConnect());
                mPresenter.toSearchCommunityList("");
            }
        });

        if (TextUtils.isEmpty(HTTPConstants.city)) {
            if (null == Hawk.get(HTTPConstants.PERMISSION_KEY)) {
                mPresenter.ShowAllowLocationDialog();
            } else {
                boolean isPermission = Hawk.get(HTTPConstants.PERMISSION_KEY);
                if (isPermission) {
                    initView();
                } else {
                    mPresenter.ShowAllowLocationDialog();
                }
            }
        } else {
            initView();
        }

        NetWorkReceiver.setOnNetConnectListener(isConnected -> showNetError(isConnected));

        noDataText.setText("抱歉，暂无相关搜索结果");
        noDataImage.setImageDrawable(getResources().getDrawable(R.mipmap.pic_emptypage_nonesearch));
    }

    private void initView() {
        mPresenter.toSearchCommunityList("");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mPresenter.init();
                recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);
                        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                            if (mPresenter.isVisBottom(recyclerView)) {
                                mPresenter.nextPage("");
                            }
                        }
                    }
                });
                showNetError(isNetConnect());
            }
        });


    }

    private void showNetError(boolean netConnect) {
        if (null != lError) {
            if (netConnect) {//进入界面获取网络状态
                if (View.VISIBLE == lError.getVisibility()) {
                    lError.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.VISIBLE);
                    tv_list_count.setText(listCount);
                }
            } else {
                if (View.GONE == lError.getVisibility()) {
                    listCount = tv_list_count.getText().toString();
                    lError.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                    setListSize(0);
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    @Override
    public void onStartAnim() {
        translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(SearchHomeActivity.this, R.anim.anim_bottom_to_top);
        translateAnimation1 = (TranslateAnimation) AnimationUtils.loadAnimation(SearchHomeActivity.this, R.anim.anim_bottom_to_top_slow);
        translateAnimation2 = (TranslateAnimation) AnimationUtils.loadAnimation(SearchHomeActivity.this, R.anim.anim_bottom_to_top_more_slow);

        rTop.startAnimation(translateAnimation);
        recyclerView.startAnimation(translateAnimation1);
        lBack.startAnimation(translateAnimation2);
        lBottom.startAnimation(translateAnimation2);
    }

    @Override
    public LinearLayout getLBack() {
        return lBack;
    }

    @Override
    public void setTvSearchText(String string) {
        Log.e("TvSearch", "setTvSearchText:" + string);
        LogUtils.e("TvSearch setTvSearchText:" + string);
        if (TextUtils.isEmpty(string)) {
            tvSearchText.setText("找不到小区？试一试搜索吧");
        } else {
            tvSearchText.setText(string);
        }
        mPresenter.setPageIndex(1);
        mPresenter.toSearchCommunityList(string);
    }

    @Override
    public ImageView getIvSearch() {
        return ivSearch;
    }


    @Override
    public void setListSize(int count) {
        tv_list_count.setText(count + "个方案结果");
    }

    @Override
    public void showNoDatas(boolean isShow) {
        if (isShow) {
            lNoData.setVisibility(View.VISIBLE);
            setListSize(0);
        } else {
            lNoData.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void noSreachData(List<SearchDefaultBean> data) {
        EventBus.getDefault().postSticky(new NoSearchDataEvent(data));
        SearchNoDataActivity.open(mContext);
        finish();
    }

    @Override
    public void locationError() {
        runOnUiThread(() -> mPresenter.ShowLocationFaildDialog());
    }

    @Override
    public void closeActicity() {
        finish();
    }

    @Override
    public void getLocationSuccess() {
        initView();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void SreachNoDataEvent(SreachNoDataEvent event) {
//        mPresenter.toSearchCommunityList("");
//        mPresenter.setIsSearchView(false);
//        tvSearchText.setText("找不到小区？试一试搜索吧");
//        ivSearch.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.l_back, R.id.l_bottom})
    public void Onclick(View view) {
        switch (view.getId()) {
            case R.id.l_back:
                if (mPresenter.isSearchView()) {
                    mPresenter.setIsSearchView(false);
                    //mPresenter.initData(null);
//                    setTvSearchText("找不到小区？试一试搜索吧");
                    mPresenter.toSearchCommunityList("");
                    tvSearchText.setText("找不到小区？试一试搜索吧");
                    ivSearch.setVisibility(View.VISIBLE);
                } else {
                    finish();
                }
                break;
            case R.id.l_bottom:
                mPresenter.showSearchDialog();
                break;
        }
    }
}
